package com.example.travel_front.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.travel_front.Activity.NursingHomeDetailActivity;
import com.example.travel_front.Model.NursingHome;
import com.example.travel_front.R;

import java.util.List;

public class NursingHomeAdapter extends RecyclerView.Adapter<NursingHomeAdapter.ViewHolder> {

    private final List<NursingHome> nursingHomeList;


    private final Context context;

    // 构造函数
    public NursingHomeAdapter(Context context, List<NursingHome> nursingHomeList) {
        this.nursingHomeList = nursingHomeList;
        this.context = context;
    }

    @NonNull
    @Override
    public NursingHomeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建视图并返回ViewHolder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nursinghome, parent, false);
        return new NursingHomeAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NursingHomeAdapter.ViewHolder holder, int position) {
        NursingHome nursingHome = nursingHomeList.get(position);
        holder.bind(nursingHome);
        // 绑定数据到视图
        holder.itemView.setOnClickListener(v -> {
            // 处理点击事件
            Intent intent = new Intent(context, NursingHomeDetailActivity.class);
            // 可以传递数据
            intent.putExtra("nursingHomeId", nursingHome.getId());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return nursingHomeList.size();
    }

    // ViewHolder 类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView nursingHomeImage;
        TextView nursingHomeName;
        TextView nursingHomeLocation;
        TextView nursingHomeFeeRange;
        LinearLayout nursingHomeRatingContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            nursingHomeImage = itemView.findViewById(R.id.nursingHomeImage);
            nursingHomeName = itemView.findViewById(R.id.nursingHomeName);
            nursingHomeLocation = itemView.findViewById(R.id.nursingHomeLocation);
            nursingHomeFeeRange = itemView.findViewById(R.id.nursingHomeFeeRange);
            nursingHomeRatingContainer = itemView.findViewById(R.id.nursingHomeRatingContainer);
        }

        public void bind(NursingHome nursingHome) {
            nursingHomeName.setText(nursingHome.getName());
            nursingHomeLocation.setText(nursingHome.getLocation());
            nursingHomeFeeRange.setText(nursingHome.getFeeRange());
            Glide.with(nursingHomeImage.getContext()).load(nursingHome.getImage()).error(R.drawable.nursinghome).into(nursingHomeImage);
            nursingHomeRatingContainer.removeAllViews(); // 清除之前的星级图标
            // 评分转换为星级图标
            float rating = nursingHome.getRating();
            int fullStars = (int) rating;
            int starWidth = 60; // 设置星星的宽度（以像素为单位）
            int starHeight = 60; // 设置星星的高度（以像素为单位）
            // 添加满星
            for (int i = 0; i < fullStars; i++) {
                nursingHomeRatingContainer.addView(createStarView(itemView.getContext(), R.drawable.ic_star, starHeight,starWidth));
            }
            // 添加空星
            int emptyStars = 5 - fullStars;
            for (int i = 0; i < emptyStars; i++) {
                nursingHomeRatingContainer.addView(createStarView(itemView.getContext(), R.drawable.ic_star_empty, starHeight,starWidth));
            }

        }
        // 用于创建星级图标的辅助方法，设置星星高度
        private ImageView createStarView(Context context, int resourceId, int starHeight ,int starWidth) {
            ImageView star = new ImageView(context);
            star.setImageResource(resourceId);
            // 设置星星宽度和高度
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                    starWidth,
                    starHeight
            );
            star.setLayoutParams(layoutParams);
            star.setLayoutParams(layoutParams);

            return star;
        }

    }

    }

